<template>
    <Modal v-model="modal.show" width="350" :title="modal.title">
        <Form ref="form" :model="formItem" :rules="ruleValidate" :label-width="100">
            <FormItem label="代码" prop="errCode"><Input v-model="formItem.errCode" placeholder="" />
            </FormItem>
            <FormItem label="名称" prop="msg"><Input v-model="formItem.msg" type="textarea" placeholder="" :rows="4" />
            </FormItem>
            <FormItem label="类别" prop="type"><Select v-model="formItem.type">
                    <Option :value="1" key="1">强制</Option>
                    <Option :value="2" key="2">疑似</Option>
                </Select>
            </FormItem>
            <FormItem label="分值" prop="score">
                <InputNumber v-model="formItem.score" placeholder="" />
            </FormItem>
        </Form>
        <template #footer>
            <div>
                <Button type="text" @click="modal.show = false">取消</Button>
                <Button type="primary" @click="handleOk">确定</Button>
            </div>
        </template>
    </Modal>
</template>
<script setup>
import http from '@/util/http'
import _ from 'lodash'
let { proxy } = getCurrentInstance()
const emit = defineEmits(['initList'])
let modal = reactive({ show: false, title: '' })
const form = ref()

let formItem = reactive({
    errCode: '',
    msg: '',
    type: 0,
    score: ''
})

const show = (type, data) => {
    form.value.resetFields()
    modal.show = true
    formItem.errID = ''
    if (type == 1) {
        modal.title = '添加'
    } else {
        var clone = _.cloneDeep(data)
        formItem = clone
        modal.title = '编辑'
    }
}

const ruleValidate = {
    errCode: [{ required: true, message: '请输入', trigger: 'blur' }],
    msg: [{ required: true, message: '请输入', trigger: 'blur' }],
    score: [{ required: true, type: 'number', message: '请输入', trigger: 'blur' }],
}

const handleOk = () => {
    form.value.validate((valid) => {
        if (valid) {
            http.post('/QC/Err/AddOrUpdate', toRaw(formItem), (response) => {
                if (response.data.flag) {
                    emit('initList')
                    proxy.$Message.success(response.data.msg)
                    modal.show = false
                } else {
                    proxy.$Message.error(response.data.msg)
                }
            })
        } else {
            return false
        }
    })
}

defineExpose({
    show,
})
</script>